package com.gzdh.dishrecognition.ui.pre;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.config.SysConstConfig;
import com.gzdh.dishrecognition.model.LcReportDto;
import com.gzdh.dishrecognition.ui.adapter.report.ScanTakeAdapter;
import com.gzdh.dishrecognition.ui.base.BasePresentation;

import java.util.ArrayList;
import java.util.List;

public class ScanTakePre extends BasePresentation {

    private RecyclerView recyclerView;

    private Context mContext;

    private ScanTakeAdapter scanTakeAdapter;

    private LinearLayout dataLinear;

    private RelativeLayout msgRv;

    private LinearLayout dateMsgLinear;

    private TextView totalNumberTv;

    private TextView totalPriceTv;

    public ScanTakePre(Context outerContext, Display display) {
        super(outerContext, display);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.pre_scan_take;
    }

    @Override
    protected void initView() {
        mContext = this.getContext();
        recyclerView = findViewById(R.id.recyclerView);
        dataLinear = findViewById(R.id.dataLinear);
        msgRv = findViewById(R.id.msgRv);
        dateMsgLinear = findViewById(R.id.dateMsgLinear);

        totalNumberTv = findViewById(R.id.total_number);
        totalNumberTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        totalPriceTv = findViewById(R.id.total_price);
        totalPriceTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
    }

    @Override
    protected void initData() {
        scanTakeAdapter = new ScanTakeAdapter(mContext, 16, 5);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(scanTakeAdapter);
    }

    @SuppressLint("NotifyDataSetChanged")
    public void showData(List<LcReportDto> list, String dinner, String totalNumber, String totalPrice) {
        msgRv.setVisibility(View.GONE);
        dataLinear.setVisibility(View.VISIBLE);
        scanTakeAdapter.setDinner(dinner);
        scanTakeAdapter.setLcReportDtoList(list);
        totalNumberTv.setText(totalNumber);
        totalPriceTv.setText(totalPrice);
        if (list.isEmpty()) {
            recyclerView.setVisibility(View.GONE);
            dateMsgLinear.setVisibility(View.VISIBLE);
        } else {
            recyclerView.setVisibility(View.VISIBLE);
            dateMsgLinear.setVisibility(View.GONE);
        }
        scanTakeAdapter.notifyDataSetChanged();
    }

    public void clear() {
        msgRv.setVisibility(View.VISIBLE);
        dataLinear.setVisibility(View.GONE);
        scanTakeAdapter.setDinner("");
        scanTakeAdapter.setLcReportDtoList(new ArrayList<>());
        totalNumberTv.setText("");
        totalPriceTv.setText("");
    }

    public void sureTake(DisplayMetrics metrics){
        Toast toast = Toast.makeText(mContext, "领餐成功！", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP | Gravity.START, 0, (int) (metrics.heightPixels * 0.1));
        toast.show();
        showData(new ArrayList<>(), "", "", "");
    }

    @Override
    public void show() {
        if (!SysConstConfig.getInstance().isAllowPreDin()) {
            super.show();
        }
    }
}
